import { create, update, remove, query } from '../../../../framework/service/index';
import { ToastTips } from '../../../../common/toastTips/tosat';

export default {

  namespace: 'partTimeDetail',

  state: {
    data: [],
    list: {},
    isAlert: false,
    isHandlWorkEnks:true,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/part-time-detail') {
          dispatch({
            type:'querySuccess',
            payload:{
              isHandlWorkEnks:true,
            }
          })
          dispatch({
            type: 'query',
            payload: {
              id: location.query.id
            }
          })
        }
      })
    },
  },

  effects: {

    //获取兼职详情
    *query({ payload }, { call, put }) {
      const { code, data, message } = yield call(query, `/api/pub/job/${payload.id}`, {});

      if (code === 200 && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data
          }
        })
        yield put({
          type:'jobConsultList/querySuccess',
          payload:{
            jobConsults: data.jobConsults,
          }
        })
      }
    },

    *onCollection({ payload }, { call, put }) {
      console.log('PartTimeDetail-model收藏onCollection');
      // const { code, data, message } = yield call(, `/api/job/${payload.id}`, {});
    },

    //报名
    *onSignUp({ payload }, { call, put, select }) {

      const { code, data, message } = yield call(create, `/api/applicant/application/${payload.id}`, {});

      // console.log('报名');
      // console.log('code',JSON.stringify(code));
      // console.log('data',JSON.stringify(data));
      // console.log('message',JSON.stringify(message));
      if ( code == 2000 ) {
        ToastTips('已报名', 2000, null);
      } else if ( code == 2010 ) {
        // Toast.info('请先完善信息', 1.5);

        yield put({
          type: 'querySuccess',
          payload: {
            isAlert: true
          }
        })

        //
        // const alertInstance = alert('提示', '请完善基本信息！', [
        //   { text: '取消', onPress: () => console.log('cancel'), style: 'default' },
        //   { text: '去完善', onPress: () => {
        //     // console.log("ok");
        //
        //   }}
        // ])
        // setTimeout(() => {
        //    // 可以调用close方法以在外部close
        //    console.log('auto close');
        //    alertInstance.close();
        //  }, 10000);

      } else if ( code == 200 ) {

        const list = yield select(({ partTimeDetail }) => partTimeDetail.list);
        // console.log('list', JSON.stringify(list));
        ToastTips('报名成功', 2000, null);
        list.application = 1;
        yield put({
          type: 'querySuccess',
          payload: {
            list
          }
        })
      }
    },

    *onFavors({ payload }, { call, put }) {

      const { code, data, message } = yield call(create, `/api/job/${payload.id}/favors`, {});
      if ( code == 200 && data ) {
        if ( payload.favors == 0 ) {

          ToastTips('已收藏', 2000, null);
        } else {
          ToastTips('取消收藏', 2000, null);
        }
        yield put({
          type: "query",
          payload: {
            id: payload.id
          }
        })
      }
    }

  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
    // querySuccess2(state, action) {
    //   const isAlert = action.payload.isAlert
    //   console.log('querySuccess2',isAlert);
    //   return { ...state, isAlert };
    // },
  }

};
